package grg.learn.compose.wanandroid.config

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import grg.learn.compose.wanandroid.viewmodels.MainViewModel

interface AppViewModelCreateFactory : ViewModelProvider.Factory {

    fun mainVm(): MainViewModel

    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        return when (modelClass) {
            MainViewModel::class.java -> mainVm()
            else -> error("")
        } as T
    }

    companion object : AppViewModelCreateFactory {

        private val repoProvider: AppRepositoryProvider = AppRepositoryProvider

        override fun mainVm(): MainViewModel {
            return MainViewModel(repoProvider.indexRepository())
        }
    }

}